/**
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package io.gitee.huangjianbo.supos.openapi.core.tools;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;

import io.gitee.huangjianbo.supos.openapi.core.errors.ApiClientException;

/**
 * @author tomcat
 * @version 1.0
 */
public final class DateTimeUtils {

    public static final String UTC0_FORMAT_STR = "yyyy-MM-dd'T'HH:mm:ss.SSSZ";
    public static final SimpleDateFormat UTC0_FORMAT = new SimpleDateFormat(UTC0_FORMAT_STR);
    public static final DateTimeFormatter UTC0_FORMATTER = DateTimeFormatter.ofPattern(UTC0_FORMAT_STR);
    public static final DateTimeFormatter UTC0_SIMPLE_FORMATTER = DateTimeFormatter.ofPattern("yyyyMMdd'T'HHmmss'Z'");

    /**
     * 获得当前的UTC0时间： 20200113T101435Z
     * 
     * @return
     */
    public static String getSimpleUTC0() {
        return getZonedUTC0().format(UTC0_SIMPLE_FORMATTER);
    }

    /**
     * 获得当前的UTC0时间： 2020-01-13T10:14:35.300
     *
     * @return
     */
    public static LocalDateTime getLocalUTC0() {
        return LocalDateTime.ofInstant(new Date().toInstant(), ZoneId.of("UTC"));
    }

    /**
     * 将UTC0时间字符串解析为LocalDateTime
     * 
     * @param formatedUTCDatetimeStr UTC0时间字符串: 2020-01-13T10:14:35.300
     * @return
     */
    public static LocalDateTime getLocalUTC0(String formatedUTCDatetimeStr) {
        try {
            Date date = UTC0_FORMAT.parse(formatedUTCDatetimeStr);
            return LocalDateTime.ofInstant(date.toInstant(), ZoneId.of("UTC"));
        } catch (ParseException e) {
            throw new ApiClientException("Can not parse to UTC: " + formatedUTCDatetimeStr, e);
        }
    }

    /**
     * 获得当前的UTC0时间： 2020-01-13T10:14:35.300Z
     *
     * @return
     */
    public static ZonedDateTime getZonedUTC0() {
        return getZonedUTC0(getLocalUTC0());
    }

    /**
     * 将LocalDateTime转换为UTC0时间
     * 
     * @param dateTime 时间
     * @return
     */
    public static ZonedDateTime getZonedUTC0(LocalDateTime dateTime) {
        return ZonedDateTime.of(dateTime, ZoneId.of("UTC"));
    }

    /**
     * 获得当前的UTC0时间： 2020-01-13T10:14:35.300+0000
     *
     * @return
     */
    public static String getUTC0() {
        return getUTC0(getZonedUTC0());
    }

    /**
     * 将ZonedDateTime转换为UTC0时间
     * 
     * @param dateTime 时间
     * @return
     */
    public static String getUTC0(ZonedDateTime dateTime) {
        return dateTime.format(UTC0_FORMATTER);
    }
}
